"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
const maishu_node_mvc_1 = require("maishu-node-mvc");
const database_1 = require("../database");
const entities_1 = require("../entities");
const errors_1 = require("../errors");
const common_1 = require("../common");
const maishu_chitu_service_1 = require("maishu-chitu-service");
const EXISTS = "_exists";
let PageDataController = class PageDataController {
    async list(conn) {
        let items = await conn.getRepository(entities_1.PageDataRecord).find();
        return items;
    }
    async add(conn, { item }, appId) {
        if (item == null)
            throw errors_1.errors.routeDataFieldNull("item");
        if (item.pageData == null)
            throw errors_1.errors.argumentFieldNull("pageData", "item");
        if (item.name == null)
            throw errors_1.errors.argumentFieldNull("name", "item");
        if (!appId)
            throw errors_1.errors.argumentNull("appId");
        if (!item.id)
            item.id = maishu_chitu_service_1.guid();
        item.applicationId = appId;
        item.createDateTime = new Date(Date.now());
        await conn.getRepository(entities_1.PageDataRecord).insert(item);
        let obj = { id: item.id, createDateTime: item.createDateTime };
        obj[EXISTS] = true;
        return obj;
    }
    async update(conn, { item }, appId) {
        if (item == null)
            throw errors_1.errors.routeDataFieldNull("item");
        if (item.pageData == null)
            throw errors_1.errors.argumentFieldNull("pageData", "item");
        if (item.id == null)
            throw errors_1.errors.argumentFieldNull("id", "item");
        if (!appId)
            throw errors_1.errors.argumentNull("appId");
        let obj = { pageData: item.pageData };
        if (item.name) {
            obj.name = item.name;
        }
        await conn.getRepository(entities_1.PageDataRecord).update({ id: item.id, applicationId: appId }, obj);
        return {};
    }
    async remove(conn, id) {
        if (!id)
            throw errors_1.errors.argumentNull("id");
        await conn.getRepository(entities_1.PageDataRecord).delete(id);
        return {};
    }
    async save(conn, { item }, appId) {
        if (item == null)
            throw errors_1.errors.routeDataFieldNull("item");
        if (item[EXISTS]) {
            delete item[EXISTS];
            return this.update(conn, { item }, appId);
        }
        if (item.id)
            return this.update(conn, { item }, appId);
        return this.add(conn, { item }, appId);
    }
    async item(conn, { name, id }, appId) {
        if (name == null && id == null)
            throw new Error("One of name or id of route data field can not be null.");
        let repository = conn.getRepository(entities_1.PageDataRecord);
        let item;
        if (name != null) {
            item = await repository.findOne({ name, applicationId: appId });
        }
        else
            item = await repository.findOne(id);
        if (item != null)
            item[EXISTS] = true;
        return item;
    }
};
__decorate([
    maishu_node_mvc_1.action(),
    __param(0, database_1.connection)
], PageDataController.prototype, "list", null);
__decorate([
    maishu_node_mvc_1.action(),
    __param(0, database_1.connection), __param(1, maishu_node_mvc_1.routeData), __param(2, common_1.currentAppId)
], PageDataController.prototype, "add", null);
__decorate([
    maishu_node_mvc_1.action(),
    __param(0, database_1.connection), __param(1, maishu_node_mvc_1.routeData), __param(2, common_1.currentAppId)
], PageDataController.prototype, "update", null);
__decorate([
    maishu_node_mvc_1.action(),
    __param(0, database_1.connection)
], PageDataController.prototype, "remove", null);
__decorate([
    maishu_node_mvc_1.action(),
    __param(0, database_1.connection), __param(1, maishu_node_mvc_1.routeData), __param(2, common_1.currentAppId)
], PageDataController.prototype, "save", null);
__decorate([
    maishu_node_mvc_1.action(),
    __param(0, database_1.connection), __param(1, maishu_node_mvc_1.routeData), __param(2, common_1.currentAppId)
], PageDataController.prototype, "item", null);
PageDataController = __decorate([
    maishu_node_mvc_1.controller("page-data")
], PageDataController);
exports.PageDataController = PageDataController;
